home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr09 / vstsrc.zip / ROTATOR.C < prev    next >
C/C++ Source or Header  |  1995-01-25  |  15KB  |  433 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack orbit calculations are based on those that appear in Dr. Manfred
  5.  * Bester's sattrack program (the Unix(tm) versions 1 and 2).
  6.  *
  7.  * The data from which the maps where generated come from "xsat", an
  8.  * X-Windows program by David A. Curry (N9MSW).
  9.  *
  10.  * Site coordinates come from various sources, including a couple of
  11.  * World Almanacs, and also from both of the programs mentioned above.
  12.  *
  13.  * The following are authors' applicable copyright notices:
  14.  *
  15.  *                                                                               
  16.  * Copyright (c) 1992, 1993, 1994 Manfred Bester. All Rights Reserved.        
  17.  *                                                                           
  18.  * Permission to use, copy, modify, and distribute this software and its      
  19.  * documentation for educational, research and non-profit purposes, without   
  20.  * fee, and without a written agreement is hereby granted, provided that the  
  21.  * above copyright notice and the following three paragraphs appear in all    
  22.  * copies.                                                                    
  23.  *                                                                              
  24.  * Permission to incorporate this software into commercial products may be    
  25.  * obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,     
  26.  * Berkeley, CA 94709, USA.                                                   
  27.  *                                                                             
  28.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,  
  29.  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF    
  30.  * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED   
  31.  * OF THE POSSIBILITY OF SUCH DAMAGE.                                         
  32.  *                                                                             
  33.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT       
  34.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    
  35.  * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"       
  36.  * BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,  
  37.  * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                   
  38.  *                                                                             
  39.  *                                                                             
  40.  * Copyright 1992 by David A. Curry                                            
  41.  *                                                                             
  42.  * Permission to use, copy, modify, distribute, and sell this software and its 
  43.  * documentation for any purpose is hereby granted without fee, provided that  
  44.  * the above copyright notice appear in all copies and that both that copyright
  45.  * notice and this permission notice appear in supporting documentation.  The  
  46.  * author makes no representations about the suitability of this software for  
  47.  * any purpose.  It is provided "as is" without express or implied warranty.   
  48.  *                                                                             
  49.  * David A. Curry, N9MSW                                                       
  50.  * Purdue University                                                           
  51.  * Engineering Computer Network                                                
  52.  * 1285 Electrical Engineering Building                                        
  53.  * West Lafayette, IN 47907                                                    
  54.  * davy@ecn.purdue.edu                                                         
  55.  *                                                                             
  56.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  57.  *
  58.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  59.  * in whole, or in part, for educational, non-profit and non-commercial use
  60.  * only, free of charge or obligation, and without agreement, provided that
  61.  * all copyrights and restrictions noted herein are observed and followed, and
  62.  * additionally, that this and all other copyright notices listed herein
  63.  * appear unaltered in all copies and in all derived work.
  64.  *
  65.  * This notice shall not in any way void or supersede any of the other authors
  66.  * rights or privileges.
  67.  *
  68.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  69.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  70.  * direct, indirect, incidental, or consequential damage, loss of profits or
  71.  * other tangible or intangible losses or benefits, arising out of or related
  72.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  73.  * but not limited to those of merchantablity and fitness for a particular
  74.  * purpose.
  75.  *
  76.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  77.  * sia@bga.com or sia@realtime.com.
  78.  */
  79.  
  80.  
  81. #include <windows.h>
  82. #include <stdio.h>
  83. #include <math.h>
  84. #include <time.h>
  85.  
  86. #include "vstdefs.h"
  87. #include "vsttype.h"
  88. #include "vstextrn.h"
  89.  
  90. exserver_t serverInfo[2] = {
  91.     {   "RadioControl",
  92.         "\\\\.\\PIPE\\_VRADIO_",
  93.         ST_SRV_NONE, 0, 0,
  94.         NULL, NULL, NULL, NULL, NULL, NULL, { 0 }, { 0 },
  95.         FmtProc
  96.     },
  97.     {   "RotatorControl",
  98.         "\\\\.\\PIPE\\_VROTAT_",
  99.         ST_SRV_NONE, 0, 0,
  100.         NULL, NULL, NULL, NULL, NULL, NULL, { 0 }, { 0 },
  101.         FmtProc
  102.     },
  103. };
  104.  
  105. static HANDLE hth;
  106.  
  107. BOOL
  108. ServerCreate(exserver_t *exp, void *vp)
  109. {
  110.     DWORD dummy;
  111.     extern int ServerStart();
  112.     extern char *ErrorString(int);
  113.     
  114.     if (vp == NULL)
  115.         return FALSE;
  116.  
  117.     exp->srv_vp = vp;
  118.     
  119.     if (currentSel)
  120.         exp->srv_sp = currentSel;
  121.     else if (selInfo)
  122.         exp->srv_sp = selInfo;
  123.     else
  124.         exp->srv_sp = NULL;
  125.  
  126.     if (exp->srv_flags & 0x2)        /* XXX read lock needed */
  127.         return FALSE;
  128.         
  129.     exp->srv_flags |= 2;            /* XXX write lock needed */    
  130.  
  131.     hth = CreateThread(NULL, 0, (PTHREAD_START_ROUTINE) ServerStart,
  132.                     (LPVOID) exp, (DWORD) 0, (DWORD *)&dummy);
  133.  
  134.     if ((hth == NULL) || (hth == INVALID_HANDLE_VALUE)) {
  135.         exp->srv_flags &= ~0x2;
  136.         sprintf(tmpbuf,"Cannot start service: %s", ErrorString(GetLastError()));
  137.         usermsg(tmpbuf);
  138.         return FALSE;
  139.     }
  140.     return TRUE;
  141. }
  142.  
  143. static BOOL
  144. /* needed to protect aginst missing reload (optimizer bug?) */
  145. ServerTerminate(exserver_t *exp)
  146. {
  147.     return (exp->srv_flags & 0x1) ? TRUE : FALSE;
  148. }
  149.  
  150. static BOOL
  151. ServerSend(exserver_t *exp, char *buf)
  152. {
  153.     BOOL status;
  154.     int written, result, error;
  155.     
  156.     status = WriteFile (exp->srv_hpipe, buf, 100,
  157.                         &written, &exp->srv_WrOverlap);
  158.     error = GetLastError();
  159.  
  160.     if (!status && error == ERROR_IO_PENDING) {
  161.         result  = WaitForSingleObject (exp->srv_WrEvent, (DWORD)-1);
  162.         if (result != WAIT_OBJECT_0) 
  163.             status = FALSE;
  164.         else
  165.             status = GetOverlappedResult(exp->srv_hpipe,
  166.                 &exp->srv_WrOverlap, &written, FALSE);
  167.     }
  168. #ifdef _DEBUG_
  169.     diag("ServerSend: returning %d\n", status);
  170. #endif
  171.     return status;
  172. }
  173.  
  174.  
  175. static int
  176. ServerStart(exp)
  177. exserver_t *exp;
  178. {
  179.     int e, i, n;
  180.     extern void ServerProc(void *);
  181.     select_t *sp;
  182.     BOOL error, flag, (*libfunc)(int, HWND, select_t *, void *);
  183.     extern BOOL ServerInit(exserver_t *);
  184.     extern void *LibFunc(const char *), LibClose(void);
  185.     extern char *ErrorString(int);
  186.  
  187.     ASSERT(exp);
  188.     if (!(sp = exp->srv_sp)) {
  189.         exp->srv_flags &= ~0x2;  /* write lock needed */
  190.         return 0;
  191.     }
  192.         
  193.     if (exp->srv_hpipe || (exp->srv_state != ST_SRV_NONE)) {
  194.         SetWindowText(Gwnd,VersionStr);
  195.         usermsg("Server already running or busy");
  196.         exp->srv_flags &= ~0x2;
  197.         return 0;
  198.     }
  199.     error = FALSE;
  200.     flag = FALSE;
  201.     
  202.     SetWindowText(Gwnd, "Trying to connect to server. Please Wait...");
  203.  
  204.     for (n = 0; n < 3; n++) {
  205.  
  206.         for (i = 0; i< 20; i++) {
  207.             exp->srv_hpipe = CreateFile (exp->srv_pipename,
  208.                 GENERIC_READ | GENERIC_WRITE,
  209.                 FILE_SHARE_READ | FILE_SHARE_WRITE , NULL,
  210.                 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
  211.  
  212.             if (exp->srv_hpipe != INVALID_HANDLE_VALUE) {
  213.                 
  214.                 exp->srv_thread_handle = CreateThread(NULL, 0,
  215.                             (PTHREAD_START_ROUTINE) ServerProc,
  216.                             (LPVOID) exp, (DWORD) 0, (DWORD *)&e);
  217.                     
  218.                 if ((exp->srv_thread_handle == INVALID_HANDLE_VALUE) ||
  219.                     (exp->srv_thread_handle == NULL) ) {
  220.                     sprintf(tmpbuf,"Cannot create service thread: %s",
  221.                         ErrorString(GetLastError()));
  222.                     usermsg(tmpbuf);
  223.                     exp->srv_thread_handle = NULL;
  224.                     error = TRUE;
  225.                     break;
  226.                 }
  227.                 exp->srv_state = ST_SRV_RUNNING;
  228.                 SetWindowText(Gwnd, VersionStr);
  229.                 exp->srv_flags &= ~0x2;
  230.                 return 0;
  231.             }
  232.             else {
  233.                 exp->srv_hpipe = NULL;
  234.                 e = GetLastError();
  235.                 if ((e == ERROR_SEEK_ON_DEVICE) || (e == ERROR_FILE_NOT_FOUND))
  236.                     Sleep(500);
  237.                 else {
  238.                     sprintf(tmpbuf, "can't open channel to control server - error code %d", error);
  239.                     usermsg(tmpbuf);
  240.                     error = TRUE;
  241.                     break;
  242.                 }
  243.             }
  244.         }
  245.         if (flag)
  246.             break;
  247.  
  248.         if (error) {
  249.             usermsg("Unable to start or connect to server");
  250.             break;
  251.         }
  252.  
  253.         if (!(libfunc = (void (*)) LibFunc(exp->srv_libfun))) {
  254.             LibClose();
  255.             usermsg(dllinvalidmsg);
  256.             break;
  257.         }
  258.  
  259.         flag = TRUE;
  260.         error = ! (*libfunc)(VSTVERSION, Gwnd, exp->srv_sp, exp->srv_vp);
  261.  
  262.         LibClose();
  263.  
  264.         if (error)
  265.             break;
  266.     }
  267.     SetWindowText(Gwnd, VersionStr);
  268.     exp->srv_flags &= ~0x2;
  269.     return 0;
  270. }
  271.  
  272.  
  273. void
  274. ServerProc(exserver_t *exp)
  275. {
  276.     select_t *sp;
  277.     BOOL    done, first;
  278.     int     nslp, when;
  279.     HANDLE  intr;
  280.     char    buf[256];
  281.     
  282. #ifdef _DEBUG_
  283.     diag("PIPE ServerProc started.\n");
  284. #endif
  285.     ASSERT(exp);
  286.  
  287.     if (!(sp = exp->srv_sp))
  288.         goto finish;
  289.         
  290.     intr = sp->rtd_thread_handle;
  291.     
  292.     exp->srv_WrEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
  293.     exp->srv_WrOverlap.hEvent = exp->srv_WrEvent;
  294.  
  295.     exp->srv_RdEvent  = CreateEvent (NULL, TRUE, FALSE, NULL);
  296.     exp->srv_RdOverlap.hEvent = exp->srv_RdEvent;
  297.  
  298.     when = 0;
  299.     done = FALSE;
  300.     first = TRUE;
  301.  
  302. #ifdef _DEBUG_
  303.     diag("Server thread started. exp %x sp %x\n", exp, sp);
  304. #endif /* _DEBUG_ */
  305.  
  306.     sprintf(buf,"> HELLO %02d 1 WH %08x SEQ %d", VSTVERSION, Gwnd,
  307.         exp->srv_seq = (time(0) & 63));
  308.         
  309.     exp->srv_seq++;
  310.     
  311.     if (ServerSend(exp, buf)) do {
  312.         
  313.         if (sp->rtd_thread_handle) {
  314.             if (currentSel && sp != currentSel) {
  315.                 sprintf(buf,"! %03d 'SAT CHANGED'", exp->srv_seq);
  316.                 first = TRUE;
  317.                 exp->srv_sp = sp = currentSel;
  318.                 intr = sp->rtd_thread_handle;
  319.             }
  320.             else {
  321.                 if (first) {
  322.                     first = FALSE;
  323.                     (*(exp->srv_fmtproc))(FMT_INIT, exp->srv_seq, exp, sp, buf);
  324.                 }
  325.                 else
  326.                     (*(exp->srv_fmtproc))(FMT_FMT, exp->srv_seq, exp, sp, buf);
  327.             }
  328.         }
  329.         else {
  330.             first = TRUE;
  331.             sprintf(buf,"! %03d 'STOPPED'", exp->srv_seq);
  332.             if (currentSel)
  333.                 exp->srv_sp = sp = currentSel;
  334.         }
  335.         exp->srv_seq++;
  336.         
  337.         done = !ServerSend(exp, buf);
  338.  
  339.         if (!done) {
  340.             nslp = sp->rtd_thread_handle ? (sp->updatetime << 1) : 20;
  341.             while (intr == sp->rtd_thread_handle && when++ < nslp)
  342.                 Sleep((DWORD) 500);
  343.             when = 0;
  344.             intr = sp->rtd_thread_handle;
  345.         }
  346.         done = done || ServerTerminate(exp);
  347.             
  348.     } while (!done);
  349.  
  350.     usermsg("External server disconnected or gone. Terminating connection.");     
  351.     CloseHandle(exp->srv_WrEvent);
  352.     DeleteObject(exp->srv_WrEvent);
  353.     CloseHandle(exp->srv_RdEvent);
  354.     DeleteObject(exp->srv_RdEvent);
  355.     exp->srv_WrEvent = NULL;
  356.  
  357.   finish:
  358.     if (exp->srv_hpipe) {
  359.         CloseHandle(exp->srv_hpipe);
  360.         exp->srv_hpipe = NULL;
  361.     }
  362.     PostMessage(Gwnd, EXTM_THEXIT, (WPARAM) 0, (LPARAM) exp->srv_thread_handle);
  363.     Sleep(0);
  364.     PostMessage(Gwnd, EXTM_THEXIT, (WPARAM) 0, (LPARAM) hth);
  365.     Sleep(0);
  366.     hth = exp->srv_thread_handle = NULL;
  367.     exp->srv_state = ST_SRV_NONE;
  368.     exp->srv_flags = 0;
  369.     ExitThread(0);
  370. }
  371.  
  372.  
  373. void
  374. FmtProc(int op, int seqq, exserver_t *exp, select_t *sp, char *buf)
  375. {
  376.     track_t *tp;
  377.     result_t *rp;
  378.     satellite_t *satp;
  379.     site_t *sitep;
  380.  
  381.     buf[0] = 0;
  382.     if (!sp)
  383.         return;
  384.         
  385.     tp = sp->sl_tp;
  386.     rp = sp->sl_rp;
  387.     
  388.     satp = tp->satp;
  389.     sitep = tp->sitep;
  390.     
  391.     switch (op) {
  392.         
  393.     case FMT_INIT:
  394.         sprintf(buf,"* %03d INIT UPD %04d MIE %3.1lf MD '%3.3s' SITE '%s' SAT '%s'",
  395.                 seqq, sp->updatetime, tp->minelevation, rp->r_modestr,
  396.                 sitep->c_name, satp->s_name);
  397.         break;
  398.  
  399.     case FMT_FMT:
  400.         if (sp->rtd_thread_handle)
  401.             rtd_lock(sp);
  402.   
  403.         sprintf(buf,"+ %03d AZ %+03.1lf EL %+03.1lf RNG %+07.0lf.0 DV %+04.7lf PL %4.1lf %s",
  404.                 seqq, rp->r_azimuth, rp->r_elevation, rp->r_range, rp->r_rangerate,
  405.                 rp->r_pathloss, satp->s_name);
  406.  
  407.         if (sp->rtd_thread_handle)
  408.             rtd_unlock(sp);
  409.  
  410.         break;
  411.  
  412.     default:
  413.         buf[0] = 0;
  414.         break;
  415.     }
  416. }
  417.  
  418. void
  419. ServerDestroy()
  420. {
  421.     exserver_t *exp;
  422.     int i;
  423.  
  424.     for (i=0,exp = &serverInfo[0]; i<2; i++) {
  425.         if (exp->srv_state != ST_SRV_NONE) {
  426.             if (exp->srv_thread_handle) {
  427.                 exp->srv_flags |= 1;        /* HACK */
  428.                 WaitForSingleObject(exp->srv_thread_handle, (DWORD)25000);
  429.             }
  430.         }
  431.     }
  432. }
  433.